#ifndef __HELLOWORLD_SCENE_H__
#define __HELLOWORLD_SCENE_H__

#include "cocos2d.h"

using namespace std;
USING_NS_CC;

enum STEP {
    STEP_DEFAULT    = 0,
    STEP_STARTPOINT = 1,
    STEP_ENDPOINT   = 2,
};

enum GRID_FLAG {
    GRID_FLAG_DEFAULT  = 0,
    GRID_FLAG_OBSTACLE = 1,
};

const int GRID_SIDELEN = 32;

#define TAG_HERO 100

class HelloWorld : public cocos2d::Layer
{
public:
    // there's no 'id' in cpp, so we recommend returning the class instance pointer
    static cocos2d::Scene* createScene();

    // Here's a difference. Method 'init' in cocos2d-x returns bool, instead of returning 'id' in cocos2d-iphone
    virtual bool init();  
    
    // a selector callback
    void menuCloseCallback(Object* pSender);
    void startPointCallback(Object* pSender);
    void endPointCallback(Object* pSender);
    void obstaclePointCallback(Object* pSender);
    
    Point getNextPoint();
    void heroMove(float dt);
    
    // implement the "static create()" method manually
    CREATE_FUNC(HelloWorld);
    
public:
    virtual void draw();
    virtual bool onTouchBegan(Touch* pTouch, Event* pEvent);
    void findPath();
private:
    void initMap();
    int getRow();
    int getCol();
    int getIndexByPoint(Point pt);
    void getRectPointByIndex(int nIndex, Point &ptLT, Point &ptRD);
    
    int m_nStartIndex;
    int m_nEndIndex;
    
    struct ST_GRID {
        ST_GRID() { gf = GRID_FLAG_DEFAULT; };
        GRID_FLAG gf;
    };
    typedef vector<ST_GRID*> VEC_GRID;
    VEC_GRID m_vecGrid;
    
    struct NODE {
        NODE() { nIndex = 0; nG = 0; pParent = NULL; };
        int nIndex;
        int nG;
        NODE* pParent;
    };
    vector<NODE*> m_vecPath;
    
    int getIndexByDir(int nIndex, int nDir);
    int getGByIndex(int nStartIndex, int nEndIndex);
    int getHByIndex(int nIndex);
    NODE *inTable(int nIndex, vector<NODE*> &vecTbl);
    
    int m_nStep;
    int m_PathNum;
};

#endif // __HELLOWORLD_SCENE_H__
